package com.wuliupai.delivery.wuliupaidelivery.utils;

import android.text.TextUtils;

import org.androidannotations.annotations.EBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间日期工具
 */

@EBean(scope = EBean.Scope.Singleton)
public class DateUtils {


//    private SimpleDateFormat mFormatterTime = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取上n个月或下n个月的第一天和最后一天
     *
     * @param baseDate          基准日期
     * @param monthInterval     月份的间隔
     *                          1：表示下一个月
     *                          -1：表示上一个月
     * @param isLessThanCurrent 最后一天是否要小于等于当前时间
     * @return
     */
    public String[] getFirstDayAndLastDayOfLastOrNextMonth(String baseDate, int monthInterval, boolean isLessThanCurrent) {

        SimpleDateFormat mFormatterTime = new SimpleDateFormat("yyyy-MM-dd");

        String[] result = {"", ""};
        if (TextUtils.isEmpty(baseDate)) {
            return result;
        }

        Date date = string2Date(baseDate);
        if (null == date) {
            return result;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, monthInterval);


        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); //设置为本月的最后一天
        Date lastDateOfMonth = calendar.getTime();

        if (isLessThanCurrent) {//如果选择时间必须小于当前时间
            long dateEndMonth = lastDateOfMonth.getTime();
            long dateCurrent = System.currentTimeMillis();
            if (dateEndMonth > dateCurrent) {
                result[1] = formatMillsecond2String(dateCurrent);
                calendar.setTime(new Date(dateCurrent));
                calendar.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
                result[0] = mFormatterTime.format(calendar.getTime());
                return result;
            }
        }

        result[1] = mFormatterTime.format(calendar.getTime());

        calendar.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
        result[0] = mFormatterTime.format(calendar.getTime());

        return result;
    }

    /**
     * 将毫秒格式化成字符串
     *
     * @param millseconds
     * @return
     */
    public String formatMillsecond2String(long millseconds) {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(millseconds));
    }

    /**
     * 字符串转换成日期
     *
     * @param dateStr
     * @return date
     */
    private Date string2Date(String dateStr) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /** ************************************************************************************************************************************************* */

    /**
     * 将时间字符串重新格式化为指定格式
     *
     * @param str_date
     * @param format
     * @return
     */
    public String formatData(String str_date, String format) {
        if (null == str_date) {
            str_date = "";
        } else {
            if (str_date.contains(".")) {
                str_date = str_date.split("\\.")[0];
            }

            str_date = new SimpleDateFormat(format).format(string2Date(str_date).getTime());
        }
        return str_date;
    }


    /** ************************************************************************************************************************************************* */

    /**
     * 获取当前小时
     */
    private int getTimeHour(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int i = cal.get(Calendar.HOUR_OF_DAY);
        return i;
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    private String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 判断当前时间是否为星期二 9点 到 22点之间
     */
    public boolean IfCurrentTimeConform(Date dt) {
        if ("星期一".equals(getWeekOfDate(dt))) {
            int hour = getTimeHour(dt);
            if (hour >= 9 && hour <= 18) {
                return true;
            }
        }
        return false;
    }
}
